package net.yambal.mobile.adv.manager
{
	import flash.utils.Dictionary;
	
	import net.yambal.mobile.adv.ADV_Chapter;
	import net.yambal.mobile.adv.ADV_Paragraph;
	import net.yambal.mobile.adv.ADV_SelectItem;
	import net.yambal.mobile.adv.ADV_SelectItemOption;
	import net.yambal.mobile.adv.ADV_TextItem;
	import net.yambal.mobile.adv.IADV_ParagraphItem;
	import net.yambal.mobile.adv.managed_component.ADV_StoryView;

	public class ADV_StoryMng
	{
		
		private static var _instance:ADV_StoryMng = null;
		
		private var _appMNG:ADV_AppMng = null;
		private var _storyView:ADV_StoryView = null;
		
		private var _storyXML:XML = null;

		private var _startChapterID:String;
		private var _chapters_dic:Dictionary;
		private var _chapters_arr:Array;
		
		
		private var _waitingChapterID:String = null;
		private var _waitingParagraphID:String = null;
		
		public function ADV_StoryMng(storyEmbedXMLCls:Class)
		{
			_appMNG = ADV_AppMng.getInstance();
			
			_storyXML = new XML( new storyEmbedXMLCls() );
			_startChapterID = _storyXML.@start_chapter_id;
			trace(_storyXML);
			
			_chapters_dic = new Dictionary();
			_chapters_arr = new Array;
			
			var chaptersXMLList:XMLList = _storyXML.descendants("chapter");
			for(var i:int = 0; i < chaptersXMLList.length(); i++){
				var chapterXML:XML = chaptersXMLList[i];
				
				var paragraphs:Dictionary = new Dictionary();
				var paragraphXMLList:XMLList = chapterXML.descendants("paragraph");
				for(var ii:int = 0; ii < paragraphXMLList.length(); ii++){
					var paragraphXML:XML = paragraphXMLList[ii];
					var paragraph:ADV_Paragraph = new ADV_Paragraph(paragraphXML.@id, paragraphXML.@next_chapter_id, paragraphXML.@next_paragraph_id);
					
					for(var iii:int = 0; iii < paragraphXML.children().length(); iii++){
						var itemXML:XML = paragraphXML.children()[iii];
						var tagName:String = String(itemXML.localName()).toLowerCase();
						
						var item:IADV_ParagraphItem = null;;
						if(tagName == "text"){
							item = new ADV_TextItem();
							item.text = itemXML.text();
							paragraph.addItem(item);
							
						}else if(tagName == "select"){
							item = new ADV_SelectItem();
							item.text = itemXML.text[0].text();
							
							for(var iiii:int = 0; iiii < itemXML.children().length(); iiii++){
								if(XML(itemXML.children()[iiii]).localName().toLowerCase() == "option"){
									var optionXML:XML = XML(itemXML.children()[iiii]);
									item.addOption(optionXML.label.text(), optionXML.@next_chapter_id, optionXML.@next_paragraph_id, optionXML.selected.text());
								}
							}

							paragraph.addItem(item);
						}
					}
					
					paragraphs[paragraph.id] = paragraph;
				}
				
				// Chapter
				var chapter:ADV_Chapter = new ADV_Chapter(chapterXML.@id
															, chapterXML.@label
															, chapterXML.@start_paragraph_id
															, chapterXML.@seen_img
															, chapterXML.@frame_img
															, paragraphs
															, isKnowChapter(chapterXML.@id)
															, chapterXML.@text_rag);
				
				_chapters_dic[chapter.id] = chapter;
				_chapters_arr.push(chapter.id);
				
				trace(chapter.toString());
			}
		}
		
		
		public static function getInstance(storyEmbedXMLCls:Class = null):ADV_StoryMng {
			if(_instance == null){
				if(storyEmbedXMLCls != null){
					_instance = new ADV_StoryMng(storyEmbedXMLCls);
				}else{
					return null;
				}
			}
			return _instance;
		}
		
		public function onStoryViewCreateComplete(story_view:ADV_StoryView):void{
			trace(this + ":onStoryViewCreateComplete");
			_storyView = story_view;
			if(_waitingChapterID != null){
				goto(_waitingChapterID, _waitingParagraphID);
			}
		}
		
		// --------------------------------------------------------------------------------------
		/**
		 * 最初から開始する
		 **/
		public function startStory():void{
			trace(this + ":startStory");
			trace("\tStart Chapter ID:" + _startChapterID);
			
			var chapter:ADV_Chapter = _chapters_dic[_startChapterID] as ADV_Chapter;
			this.goto(_startChapterID, chapter.start_paragraph_id);
		}
		
		/**
		 * 指定したChapter/Paragraphに移動する
		 **/
		public function goto(chapter_id:String, paragraph_id:String = null, option:Object = null):void{
			trace(this + ":goto(" + chapter_id + "," + paragraph_id + ")");
			
			_waitingChapterID = null;
			_waitingParagraphID = null;
			
			// AppMngに通知する
			trace("\tAppMNGのに通知");
			_appMNG.pushChapterParagraph(chapter_id, paragraph_id, option);
			
			// storyViewに通知する
			if(_storyView == null){
				trace("\tstoryViewのCreation待ち");
				_waitingChapterID = chapter_id;
				_waitingParagraphID = paragraph_id;
				
			}else{
				trace("\tstoryViewに通知");
				_storyView.pushChapterParagraph(chapter_id, paragraph_id, option);
			}
			
			addKnownChapter(chapter_id);
			var chapter:ADV_Chapter = getChapter(chapter_id);
			chapter.known = true;
			_chapters_dic[chapter_id] = chapter;
			
			trace("chapers - - - -\n" + getAllChapters().join("\n") + "\n- - - -");
		}
		
		public function getChapter(id:String):ADV_Chapter{
			return _chapters_dic[id];
		}

		
		// Known Chapter
		public function isKnowChapter(chapter_id:String):Boolean{
			var known:Array = _appMNG.loadArray("know_chapter");
			
			trace("isKnowChapter:" + chapter_id);
			trace("\t" + known);
			trace("\t" + known.indexOf(chapter_id));
			
			if(known == null|| known.length == 0 || known.indexOf(chapter_id) >= 0){
				trace("\tun known");
				return false;
			}
			trace("\tknown");
			return true;
		}
		
		public function addKnownChapter(chapter_id:String):void{
			trace("addKnownChapter:" + chapter_id);
			if(!isKnowChapter(chapter_id)){
				trace("\tadd");
				_appMNG.pushArray("know_chapter", chapter_id);
				return;
			}
			trace("\talready known");
		}
		
		public function getAllChapters():Array{
			var ret:Array = new Array();
			for(var i:int = 0; i < _chapters_arr.length; i++){
				var id:String = _chapters_arr[i];
				var chapter:ADV_Chapter = _chapters_dic[id];
				ret.push(chapter);
			}
			return ret;
		}
	}
}